Completed
Push — master ( 9a2275...38ec3f )
by Jean
57s
created

failcompile.js ➔ parse   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 24
rs 8.6845
1
/**
2
 * @file Module 'codingame/parsers/failcompile'
3
 * @author woshilapin <[email protected]>
4
 */
5
/**
6
 * Parse failed response from Codingame when test found a value different from the compile.
7
 *
8
 * The typical response in this case if of the following form.
9
 * ```json
10
 * {
11
 * 	"success": {
12
 * 		"error": {
13
 * 			message": `SyntaxError: EOL while scanning string literal`,
14
 * 			"stacktrace": [{
15
 * 				"location": `ANSWER`,
16
 * 				"container": `Answer.py`,
17
 * 				"function": ` not in a function`,
18
 * 				"line": 26
19
 * 			}]
20
 * 		}
21
 * 	}
22
 * }
23
 * ```
24
 * @module codingame/parsers/failcompile
25
 */
26
import CodingameError from '../error.js';
27
28
let name = `fail-compile`;
29
30
/**
31
 * Attempt to parse the body of a successful request to Codingame test API.
32
 * This function will try to map a response for a failed test because the compilation of bundle failed.
33
 *
34
 * @function parse
35
 * @param {Object} body Body of the response
36
 * @returns {Promise<CodingameError>} Reject with a CodingameError if parsing was successful
37
 * @throws {Error} Throw is parsing failed
38
 * @instance
39
 */
40
let parse = function parse(body) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable parse already seems to be declared on line 40. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
41
	try {
42
		let {
43
			"error": {
44
				"message": message,
45
				"stacktrace": stacktrace
46
			}
47
		} = body;
48
		if (Array.isArray(stacktrace)) {
0 ignored issues
show
Bug introduced by
The variable stacktrace seems to be never declared. If this is a global, consider adding a /** global: stacktrace */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
49
			message += `\n`;
0 ignored issues
show
Bug introduced by
The variable message seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.message.
Loading history...
Bug introduced by
The variable message seems to be never declared. If this is a global, consider adding a /** global: message */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
50
			for (let st of stacktrace) {
51
				message += `\t${st.container}:${st.line}:${st.function}`;
52
			}
53
			let error = new CodingameError(message);
0 ignored issues
show
Comprehensibility introduced by
You are assigning a value to the catch variable error, thereby shadowing the original value.
Loading history...
Comprehensibility Naming Best Practice introduced by
The variable error already seems to be declared on line 58. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
54
			return Promise.reject(error);
55
		} else {
56
			throw `Stacktrace in the error should be an array`;
57
		}
58
	} catch(error) {
59
		let message = `The body is not of response type '${name}'\n`;
60
		message += error.message;
61
		throw new Error(message);
62
	}
63
};
64
65
export default {
66
	"name": name,
67
	"parse": parse
68
};
69